﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using SimpleReminder.Properties;
using System.Reflection;
using System.IO;
using System.Configuration;
using System.Threading;
using SimpleUtils;
using SimpleUtils.Win;

namespace SimpleReminder
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);

            string instanceName = "SimpleReminder";
            using (SimpleOneInstance soi = new SimpleOneInstance(instanceName))
            {
                if (!soi.IsFirstInstance)
                {
                    SimpleMessage.ShowInfo("Simple Reminder is already running.");
                    Application.Exit();
                    return;
                }

                try
                {
                    Settings.Default.Upgrade();

                    //try to remove previous setting
                    LocalFileSettingsProvider lfsp = Settings.Default.Providers["LocalFileSettingsProvider"] as LocalFileSettingsProvider;
                    SimpleSettings.RemovePreviousConfigFile(lfsp);
                }
                catch (Exception ex)
                {
                    SimpleMessage.ShowException(ex);
                }

                try
                {
                    if (Settings.Default.ShowAbout)
                    {
                        Utils.ShowAbout();
                        Settings.Default.ShowAbout = false;
                        Settings.Default.Save();
                    }

                    if (Settings.Default.CheckUpdateEnabled)
                    {
                        TimeSpan ts = DateTime.Now - Settings.Default.CheckUpdateLastDate;
                        if (ts.Days >= Settings.Default.CheckUpdatePeriod)
                        {
                            Utils.CheckForUpdate(true);
                            Settings.Default.CheckUpdateLastDate = DateTime.Now;
                            Settings.Default.Save();
                        }
                    }
                }
                catch (Exception ex)
                {
                    SimpleMessage.ShowException(ex);
                }


                try
                {
                    _mainForm = new frmMain();
                    Program.TimerInterval = Settings.Default.TimerInterval;

                    Application.Run(_mainForm);
                }
                catch (Exception ex)
                {
                    SimpleMessage.ShowException(ex);
                }
            }
        }

        static frmMain _mainForm = null;

        public static frmMain MainForm
        {
            get { return _mainForm; }
        }

        public static int TimerInterval
        {
            get { return _mainForm == null ? 0 : _mainForm.Timer.Interval; }
            set { if (_mainForm != null) _mainForm.Timer.Interval = value; }
        }    

        static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
        {
            SimpleMessage.ShowException(e.Exception);
        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            SimpleMessage.ShowException(e.ExceptionObject as Exception);
        }

    }//end of class
}